<template>
  <el-dialog v-bind="dialog" :visible.sync="dialog.visible" @close="close">
    <el-form ref="bindEmailForm" :model="bindEmailForm" :rules="formRules" label-position="top">
      <el-form-item prop="email" label="邮箱">
        <el-input v-model="bindEmailForm.email" size="medium" placeholder="绑定的邮箱" name="email" type="text" />
      </el-form-item>
      <el-form-item prop="code" label="验证码">
        <div class="flex">
          <el-input v-model="bindEmailForm.code" size="medium" maxlength="6" type="text" name="code" placeholder="短信验证码" />
          <SendCode ref="sendBindcode" @run="sendBindCode" />
        </div>
      </el-form-item>
      <el-form-item align="right">
        <el-button type="primary" size="mini" @click="bind">绑 定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import { validateMail } from '@/utils/validate.js'
import SendCode from '@/components/SendCode'
import { ucApi } from '@/api/api.js'

export default {
  components: {
    SendCode
  },
  data() {
    return {
      // 绑定
      bindEmailForm: {
        email: '',
        code: ''
      },
      dialog: {
        visible: false,
        closeOnPressEscape: false,
        closeOnClickModal: false
      },
      formRules: {
        email: [
          { required: true, trigger: 'blur', message: '请输入邮箱' },
          { trigger: 'blur', validator: validateMail }
        ],
        code: [{ required: true, trigger: 'blur', message: '请输入邮件验证码' }]
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  methods: {
    close() {
      this.$refs['unbindEmailForm'].resetFields()
      this.$refs['bindEmailForm'].resetFields()
    },

    // 发送绑定验证码
    sendBindCode() {
      this.$refs['bindEmailForm'].validateField('email', (valid) => {
        if (!valid) {
          this.$request(ucApi.sendBindEmailCode, { email: this.bindEmailForm.email }).then(() => {
            this.$refs.sendBindcode.start()
            this.$notify({
              title: '成功',
              message: '验证码已发送，请及时查收',
              //message: `${res.msg}`,
              type: 'success'
            })
          })
        } else {
          return false
        }
      })
    },
    // 绑定
    bind() {
      this.$refs.bindEmailForm.validate(async (valid) => {
        if (valid) {
          if (valid) {
            this.loading = true
            this.$request(ucApi.bindEmail, { ...this.bindEmailForm })
              .then((res) => {
                this.loading = false
                if (res.code === 1) {
                  this.$message({
                    message: '绑定成功',
                    type: 'success'
                  })
                  this.$store.dispatch('user/getInfo')
                  this.dialog.visible = false
                }
              })
              .catch(() => {
                this.loading = false
              })
          } else {
            return false
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-dialog__wrapper ::v-deep.el-dialog {
  max-width: 364px;
}
</style>
